//
//  HomeSingInView.m
//  PlayCatchGame
//
//  Created by 流言 on 2018/1/26.
//  Copyright © 2018年 清正. All rights reserved.
//

#import "HomeSingInView.h"
#import "SingInItemView.h"


@interface HomeSingInView() {
    NSMutableArray *itemList;
}

@property (nonatomic, strong) NSMutableArray *itemArray; // 遗留问题
@property (nonatomic, copy) ButtonBlock clickBlock;

@end


@implementation HomeSingInView {
    NSArray *titleArray;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    itemList = [NSMutableArray array];
    
    titleArray =@[@"第一天", @"第二天", @"第三天", @"第四天", @"第五天", @"第六天", @"第七天"];

    NSInteger rowCount =3;
    CGFloat itemSpace =20.0;
    CGFloat itemWidth =(CGRectGetWidth(self.contentView.frame)-itemSpace*(rowCount-1)) / (rowCount*1.0);
    CGFloat itemHeight =itemWidth*1.15;

    for (NSInteger i =0; i<titleArray.count; i++) {
        SingInItemView *itemView =[[[NSBundle mainBundle]loadNibNamed:@"SingInItemView" owner:nil options:nil] firstObject];
        itemView.titleLabel.text =titleArray[i];
        itemView.moreDay =i+1;
        if (i ==0) {
            itemView.isSiginType =YES;
        }
        [self.contentView addSubview:itemView];
        [self.itemArray addObject:itemView];
        [itemList addObject:itemView];
        
        [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
            if (i ==titleArray.count-1) {
                make.top.mas_equalTo(self.contentView.mas_top).offset(3 + i/rowCount*(2+itemHeight));
                make.centerX.mas_equalTo(self.contentView.mas_centerX).offset(0);
                make.width.mas_equalTo(itemWidth+15);
                make.height.mas_equalTo(itemHeight);
            } else {
                make.top.mas_equalTo(self.contentView.mas_top).offset(3 + i/rowCount*(2+itemHeight));
                make.left.mas_equalTo(self.contentView.mas_left).offset(5+i%rowCount*(itemWidth+itemSpace));
                make.width.mas_equalTo(itemWidth);
                make.height.mas_equalTo(itemHeight);
            }
        }];
    }
    
    [self _signRecord];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    if (!CGRectEqualToRect(CGRectZero, self.viewFrame) ) {
        self.frame =self.viewFrame;
    }
    
    NSInteger rowCount =3;
    CGFloat itemSpace =20.0;
    CGFloat itemWidth =(CGRectGetWidth(self.contentView.frame)-itemSpace*(rowCount-1)) / (rowCount*1.0);
    CGFloat itemHeight =itemWidth*1.2;
    for (NSInteger i =0; i<self.itemArray.count; i++) {
        SingInItemView *itemView =self.itemArray[i];
        [itemView mas_makeConstraints:^(MASConstraintMaker *make) {
            if (i ==titleArray.count-1) {
                make.top.mas_equalTo(self.contentView.mas_top).offset(3 + i/rowCount*(2+itemHeight));
                make.centerX.mas_equalTo(self.contentView.mas_centerX).offset(0);
                make.width.mas_equalTo(itemWidth+15);
                make.height.mas_equalTo(itemHeight);
            } else {
                make.top.mas_equalTo(self.contentView.mas_top).offset(3 + i/rowCount*(2+itemHeight));
                make.left.mas_equalTo(self.contentView.mas_left).offset(i%rowCount*(itemWidth+itemSpace));
                make.width.mas_equalTo(itemWidth);
                make.height.mas_equalTo(itemHeight);
            }
        }];
    }
}

#pragma mark - Request Network
- (void)_confirmSignRecord {
    [HttpUrl POST:@"signRecord/confirmSignRecord" dict:nil hud:self isShow:YES WithSuccessBlock:^(id data) {
        if (BB_isSuccess) {
            BOOL isSign = [data[@"data"] boolValue];
            if (isSign) {
                
            } else {
                
            }
            
        } else {
            ShowAlertTitle(@"获取签到信息失败");
        }
    }];
}

- (void)_signRecord{
    [HttpUrl GET:@"signRecord/signNumber"dict:nil hud:self isShow:YES WithSuccessBlock:^(id data) {
        if (BB_isSuccess) {
            NSInteger signInDay = [[data[@"data"] description] integerValue];
            for (int i = 0; i < itemList.count ; i++) {
                SingInItemView *view = itemList[i];
                if (i < signInDay) {
                    view.isSiginType = YES;
                }
            }
        } else {
            ShowAlertTitle(@"获取签到信息失败");
        }
    }];
}


- (NSMutableArray *)itemArray {
    if (_itemArray) {
        _itemArray =[NSMutableArray array];
    }
    return _itemArray;
}
- (IBAction)closeButtonClick:(UIButton *)sender {
    if (self.clickBlock) {
        self.clickBlock(sender);
    }
}

- (IBAction)singinButtonClick:(UIButton *)sender {
    if (self.clickBlock) {
        self.clickBlock(sender);
    }
}


- (void)viewButtonClickBlock:(ButtonBlock)newBlock {
    self.clickBlock = newBlock;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
